<?php
class Order_Model_History
{

	private $_table;
	//Initalizing Db Table  
	public function __construct(){
		$this->_table = new Order_Model_DbTable_History();
	}

	/**
	*Create A Record  
	*@param rowSet 
	*@return boolean
	*/
	public  function create($row){
 		return $this->_table->insert($row); 
	}

	/**
	*Update One Row  
	*@param rowSet $row 
	*@param int $osh_id 
	*@return boolean
	*/
	public  function update($row,$id){
		$where = $this->_table->getAdapter()->quoteInto("osh_id= ?", $id);
		return $this->_table->update($row, $where);
	}
	/**
	*Delete One Row
	*@param int $osh_id 
	*@return boolean
	*/
	public function delete($id){
		$where = $this->_table->getAdapter()->quoteInto("osh_id= ?", $id);
		return $this->_table->delete($where);
	}

    public function getByCondition($condition=array(),$type='*',$orderby=array('osh_id'),$pageSize=0,$page=1){
		$select = $this->_table->getAdapter()->select();
		$table = $this->_table->info('name');
		$select->from($table,$type);
		$select->where("1 =?",1);
		if(isset($condition["orders_code"]) && $condition["orders_code"]!=''){
			$select->where(" orders_code = ?",$condition["orders_code"]);
		}
		if(isset($condition["osh_id"]) && $condition["osh_id"]!=''){
			$select->where(" osh_id = ?",$condition["osh_id"]);
		}
		if(isset($condition["osh_type"]) && $condition["osh_type"]!=''){
			$select->where(" osh_type = ?",$condition["osh_type"]);
		}
		if(isset($condition["osh_statu"]) && $condition["osh_statu"]!=''){
			$select->where(" osh_statu = ?",$condition["osh_statu"]);
		}
		if(isset($condition["osh_create_date"]) && $condition["osh_create_date"]!=''){
			$select->where(" osh_create_date = ?",$condition["osh_create_date"]);
		}
		if(isset($condition["user_id"]) && $condition["user_id"]!=''){
			$select->where(" user_id = ?",$condition["user_id"]);
		}
		if('count(*)'==$type){
			return $this->_table->getAdapter()->fetchOne($select);
		}else{
			$select->order($orderby);
			if($pageSize>0 and $page>0)
			{
				$start =($page-1)*$pageSize;
				$select->limit($pageSize,$start);
			}
			$sql=$select->__toString();
			return $this->_table->getAdapter()->fetchAll($sql);
		}
	}
	
    public  function getOperator($condition=array()){
    	
    	$select = $this->_table->getAdapter()->select();
    	$table = $this->_table->info('name');
    	$select->from($table,'*');
    	$select->where("1 =?",1);
    	if(isset($condition["orders_code"]) && $condition["orders_code"]!=''){
    		$select->where(" orders_code = ?",$condition["orders_code"]);
    	}

    	if(isset($condition["osh_type"]) && $condition["osh_type"]!=''){
    		$select->where(" osh_type = ?",$condition["osh_type"]);
    	}
    	if(isset($condition["osh_statu"]) && $condition["osh_statu"]!=''){
    		$select->where(" osh_statu = ?",$condition["osh_statu"]);
    	}

    	if(isset($condition["osh_statu_now"]) && $condition["osh_statu_now"]!=''){
    		$select->where(" osh_statu_now = ?",$condition["osh_statu_now"]);
    	}
    	if('count(*)'=="*"){
    		return $this->_table->getAdapter()->fetchOne($select);
    	}else{
    		//osh_create_date
    		$orderby=array("osh_create_date desc");
    		$select->order($orderby);
    		$select->limit(1,0);
    		
    		$sql=$select->__toString();
    		$row=$this->_table->getAdapter()->fetchRow($sql);
    		$operName=null;
    		if($row){
    			$operNameRow=User_Service_User::getByUserId($row['user_id']);
    			$operName=$operNameRow['name'];
    		}
    		
    		return $operName;
    	}
    	
    }
	
	
	

	/**
	*Get Rowset  
	*@param string $osh_id 
	*@return Rowset
	*/
	public function getById($id){
		$where = $this->_table->getAdapter()->quoteInto('osh_id= ?', $id);
		return $this->_table->fetchRow($where);
	}
	
	public function getByfield($field=1,$value=0){
		$where = $this->_table->getAdapter()->quoteInto("{$field} = ?",$value);	
		return $this->_table->fetchAll($where);
	}
	
	
}